package org.kgduyjj.test;

import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;

import org.apache.log4j.Logger;
import org.junit.Test;
import org.kgduyjj.toolbox.DateUtil;
import org.kgduyjj.toolbox.IdcardUtil;
import org.kgduyjj.toolbox.bean.SimpleRedisKey;

/**
 * 测试主程序
 * 
 * @author K.y
 *
 */
public class Domain {
    private static Logger logger = Logger.getLogger(Domain.class);
    protected static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'+'SSSS");
    protected static final String RANDOM_NUMBERS = "0123456789";

    @Test
    public void testDate() {
        String dateStr = "2020-12-16T05:21:50+0000";
        Date date = null;
        try {
            date = DATE_FORMAT.parse(dateStr);
        } catch (ParseException e) {
            System.err.println(e.getMessage());
        }

        System.out.println(DateUtil.format(date));
    }

    @Test
    public void buildRedisKey() {
        SimpleRedisKey key = new SimpleRedisKey.Builder("tn", "sk", "sv").supplyColumn("col1").supplyColumn("col2")
                .build();
        String keyString = key.generate();
        System.out.println(keyString);
        SimpleRedisKey rebuildKey = SimpleRedisKey.rebuild(keyString);
        System.out.println(rebuildKey.generate());
    }

    @Test
    public void idCardTest() throws UnsupportedEncodingException {
        List<String> idCards = new ArrayList<String>();
        String idCard = null;
        int idCardCount = 1000;
        for (int j = 0; j < idCardCount; j++) {
            idCard = IdcardUtil.genCNCard("410702", "19920704", genSeq(3, null));
            idCards.add(idCard);
        }
        Map<String, Integer> probability = new HashMap<String, Integer>();
        for (String showIdCard : idCards) {
            System.out.println(showIdCard);
            String penultimate = IdcardUtil.getPenultimate(showIdCard);
            switch (penultimate) {
            case "2":
                if (probability.containsKey("2")) {
                    Integer count = probability.get("2");
                    count++;
                    probability.put("2", count);
                } else {
                    probability.put("2", 1);
                }
                break;
            case "4":
                if (probability.containsKey("4")) {
                    Integer count = probability.get("4");
                    count++;
                    probability.put("4", count);
                } else {
                    probability.put("4", 1);
                }
                break;
            case "6":
                if (probability.containsKey("6")) {
                    Integer count = probability.get("6");
                    count++;
                    probability.put("6", count);
                } else {
                    probability.put("6", 1);
                }
                break;
            case "8":
                if (probability.containsKey("8")) {
                    Integer count = probability.get("8");
                    count++;
                    probability.put("8", count);
                } else {
                    probability.put("8", 1);
                }
                break;
            case "0":
                if (probability.containsKey("0")) {
                    Integer count = probability.get("0");
                    count++;
                    probability.put("0", count);
                } else {
                    probability.put("0", 1);
                }
                break;
            default:
                break;
            }

        }
        System.out.println(idCards.size());
        for (Entry<String, Integer> entry : probability.entrySet()) {
            System.out.println(entry.getKey() + ":" + entry.getValue());
        }
//		String idCard = "410702199207042511";
//		boolean isCNCard = IdcardUtil.validateCNCard(idCard);
//		System.out.println(isCNCard);
    }

    @Test
    public void objectTest() {
        logger.debug("objectTest");
        // Person cZhangsan = ObjectUtil.clone(zhangsan);
        // System.out.println(cZhangsan);
    }

    /**
     * 生成指定位数随机数
     * 
     * @param len
     * @param isEvenNumber
     * @return
     */
    protected static String genSeq(int len, Boolean isEvenNumber) {
        Random r = new Random();
        StringBuilder rs = new StringBuilder();
        String nextNumber;
        for (int j = 0; j < len; j++) {
            nextNumber = String.valueOf(RANDOM_NUMBERS.charAt(r.nextInt(10)));
            rs.append(nextNumber);
        }
        return rs.toString();
    }
}
